﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class DeadLine
    {
        /// <summary>
        /// 添加截止日期信息
        /// </summary>
        /// <param name="newDeadLineInfo">截止日期对象</param>
        public string Add(DeadLineInfo newDeadLineInfo)
        {
            GMS.ORM.Model.PDeadLine myPDeadLine = new GMS.ORM.Model.PDeadLine();
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            if (optPDeadLine.Exists(newDeadLineInfo.gyear)) return "该年份截止日期信息已存在";
            else
            {
                myPDeadLine.gyear = newDeadLineInfo.gyear;
                myPDeadLine.inUse = newDeadLineInfo.inUse;
                myPDeadLine.deadline1 = newDeadLineInfo.deadline1;
                myPDeadLine.deadline2 = newDeadLineInfo.deadline2;
                myPDeadLine.deadline3 = newDeadLineInfo.deadline3;
                myPDeadLine.deadline4 = newDeadLineInfo.deadline4;
                myPDeadLine.deadline5 = newDeadLineInfo.deadline5;
                myPDeadLine.deadline6 = newDeadLineInfo.deadline6;

                //如果设置了inUse为1，则要将其余inUse全部置为0
                if (myPDeadLine.inUse == 1)
                    SetAllInUseZero();


                bool ok = optPDeadLine.Add(myPDeadLine);
                if (!ok) return ErrorMessage.DBOptError;
                else return ErrorMessage.OK;
            }
        }

        /// <summary>
        /// 删除截止日期信息
        /// </summary>
        /// <param name="graduateYear">毕业年份</param>
        public string Delete(string graduateYear)
        {
            if (!DeadLine.Exist(graduateYear))
                return ErrorMessage.NoSuchData;

            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            bool ok = optPDeadLine.Delete(graduateYear);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 更新截止日期信息
        /// </summary>
        /// <param name="updateDeadLineInfo">截止日期对象</param>
        public string Update(DeadLineInfo updateDeadLineInfo)
        {
            GMS.ORM.Model.PDeadLine myPDeadLine = new GMS.ORM.Model.PDeadLine();
            myPDeadLine.gyear = updateDeadLineInfo.gyear;
            myPDeadLine.inUse = updateDeadLineInfo.inUse;
            myPDeadLine.deadline1 = updateDeadLineInfo.deadline1;
            myPDeadLine.deadline2 = updateDeadLineInfo.deadline2;
            myPDeadLine.deadline3 = updateDeadLineInfo.deadline3;
            myPDeadLine.deadline4 = updateDeadLineInfo.deadline4;
            myPDeadLine.deadline5 = updateDeadLineInfo.deadline5;
            myPDeadLine.deadline6 = updateDeadLineInfo.deadline6;

            //如果设置了inUse为1，则要将其余inUse全部置为0
            if (myPDeadLine.inUse == 1)
                SetAllInUseZero();
            
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            bool ok = optPDeadLine.Update(myPDeadLine);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 将所有InUse置为0
        /// </summary>
        private bool SetAllInUseZero()
        {
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            DataSet ds = optPDeadLine.GetList("inUse=1");
            int count = ds.Tables[0].Rows.Count;
            if (count == 0)
                return true;
            ArrayList gyearArray = new ArrayList();
            //获得所有的gyear，其inUse为1
            for (int i = 0; i < count; i++)
                gyearArray.Add(ds.Tables[0].Rows[i]["gyear"].ToString());
            //将所有gyearArraylist中的DeadLine数据的inUse置为0
            for (int i = 0; i < gyearArray.Count; i++)
            {
                GMS.ORM.Model.PDeadLine myPDeadLine = new GMS.ORM.Model.PDeadLine();
                //拿
                myPDeadLine = optPDeadLine.GetModel(gyearArray[i].ToString());
                //改
                myPDeadLine.inUse = 0;
                //放
                bool ok = optPDeadLine.Update(myPDeadLine);
                if (!ok) return false;
            }
            return true;
        }

        /// <summary>
        /// 获得截止日期的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            return optPDeadLine.GetList("1=1 ORDER BY gyear DESC");
        }

        #region StaticFunctions

        /// <summary>
        /// 获取某个毕业文件的截止时间，如果不正确则返回null
        /// </summary>
        /// <param name="style">类型：1－6</param>
        static public DateTime? GetDeadLineDate(int style)
        {
            //如果没有设置本届毕业年份，则退出
            if (DeadLine.GetDeadLineYear() == ErrorMessage.NoSuchData)
                return null;

            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            switch (style)
            {
                case 1: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline1;
                case 2: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline2;
                case 3: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline3;
                case 4: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline4;
                case 5: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline5;
                case 6: return optPDeadLine.GetModel(DeadLine.GetDeadLineYear()).deadline6;
                default: return null;
            }
        }

        /// <summary>
        /// 本届毕业年份，如果没有设定，返回ErrorMessage.NoSuchData
        /// </summary>
        static public string GetDeadLineYear()
        {
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            DataSet ds = optPDeadLine.GetList("inUse=1");
            if (ds.Tables[0].Rows.Count == 1)
                return ds.Tables[0].Rows[0]["gyear"].ToString();
            else
                return ErrorMessage.NoSuchData;
        }

        /// <summary>
        /// 某毕业年份是否存在
        /// </summary>
        /// <param name="graduateYear">毕业年份</param>
        static public bool Exist(string graduateYear)
        {
            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            return optPDeadLine.Exists(graduateYear);
        }

        /// <summary>
        /// 获取一个截止时间的对象，如果不存在返回null
        /// </summary>
        /// <param name="graduateYear">毕业年份</param>
        static public DeadLineInfo GetDeadLineInfo(string graduateYear)
        {
            if (!DeadLine.Exist(graduateYear))
                return null;

            GMS.ORM.DAL.PDeadLine optPDeadLine = new GMS.ORM.DAL.PDeadLine();
            GMS.ORM.Model.PDeadLine myPDeadLine = new GMS.ORM.Model.PDeadLine();
            myPDeadLine = optPDeadLine.GetModel(graduateYear);

            DeadLineInfo returnDeadLineInfo = new DeadLineInfo();
            returnDeadLineInfo.gyear = myPDeadLine.gyear;
            returnDeadLineInfo.inUse = myPDeadLine.inUse;
            returnDeadLineInfo.deadline1 = myPDeadLine.deadline1;
            returnDeadLineInfo.deadline2 = myPDeadLine.deadline2;
            returnDeadLineInfo.deadline3 = myPDeadLine.deadline3;
            returnDeadLineInfo.deadline4 = myPDeadLine.deadline4;
            returnDeadLineInfo.deadline5 = myPDeadLine.deadline5;
            returnDeadLineInfo.deadline6 = myPDeadLine.deadline6;

            return returnDeadLineInfo;
        }

        /// <summary>
        /// 获取当前正在使用的截止时间的对象，如果不存在返回null
        /// </summary>
        static public DeadLineInfo GetDeadLineInfoInUse()
        {
            try
            {
                return DeadLine.GetDeadLineInfo(DeadLine.GetDeadLineYear());
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                return null;
            }
        }

        #endregion StaticFunctions
    }
}
